const extend = require('extend');

const Dispatcher = require('./dispatcher');
const Role = require('./role');

class Group extends Dispatcher {
    constructor(server, game, index, option, roles) {
        super();
        this.server = server;
        this.game = game;
        this.index = index;
        this.option = option;
        this.roles = roles.filter(role => role instanceof Role);
        this.roles.forEach((role, position) => {
            role.group = index;
            role.position = position;
        });
    }
    exit(role) {
        this.roles[role.index].off(this);
        this.roles.splice(role.index, 1);
    }
    start() {
        this.roles.forEach(role => {
            role.on(() => this.dispatch(), this);
            role.start();
        });
    }
    get data() {
        return extend(true, this.summary, {
            option: this.option,
            roles: this.roles.map(role => role.summary)
        });
    }
    get summary() {
        return {
            index: this.index
        };
    }
}

module.exports = Group;